const fs = require('fs')
const path = require('path')
    //数据model那边暴露的是对象，所以这边接受也要以对象的形式接受{add}
const { add, find, remove } = require('../model/position_add_m.js');



const position_add_c = {
    add: ({ req, res }) => { //对象，按需传入
        req.body.companyLogo = req.fileName;
        let result = add({...req.body });
        //render 模板渲染position_add 是view那个页面
        res.render('position_add', {
            data: result
        })
    },
    find: async({ req, res }) => {
        // find()==P1;
        let result = await find().then(res => res);
        res.render('position_find', {
            data: JSON.stringify(result)
        })
    },
    remove: async({ req, res }) => {
        //删除图片 upload


        let { id, companyLogo } = req.body
        let result = await remove(id).then(res => res);
        if (result) {
            fs.unlink(path.resolve(__dirname, `../public/upload/${companyLogo}`), (err) => {
                console.log('本地数据库中的图片已经删除了')
            })
            res.render('position_remove', {
                data: JSON.stringify({
                    rat: true,
                    data: {
                        msg: 'del succ'
                    }
                })
            })
        } else {
            res.render('position_remove', {
                data: JSON.stringify({
                    rat: false,
                    data: {
                        msg: 'del fail'
                    }
                })
            })
        }

    }
}



module.exports = position_add_c;